//
//  HomeViewController.swift
//  DouYuZB
//
//  Created by 刘耀 on 2017/4/11.
//  Copyright © 2017年 深圳多诺信息科技有限公司. All rights reserved.
//

import UIKit

private let kTitleViewH: CGFloat = 44


/// 主控制器
class HomeViewController: BaseViewController {

    // MARK: - 懒加载
    fileprivate lazy var pageTitleVIew: PageTitleView = { [weak self] in
       let titleFrame = CGRect(0, kNavBarH + kStatusBarH, kScreenW, kTitleViewH)
        let titles = ["推荐", "游戏", "娱乐", "趣玩"]
        let titleView = PageTitleView(frame: titleFrame, titles: titles)
        titleView.delegate = self
        return titleView
    }()
    
    fileprivate lazy var pageContentView: PageContentView = { [weak self] in
        let h = kScreenH - kStatusBarH - kNavBarH - kTitleViewH - kTabBarH
       let fm = CGRect(0, kStatusBarH + kTitleViewH + kNavBarH, kScreenW, h)
        var childVcs = [UIViewController]()
        
        childVcs.append(RecommendViewController())
        childVcs.append(GameViewController())
        childVcs.append(AmuseViewController())
        childVcs.append(FunnyViewController())
        
        let contentV = PageContentView(frame: fm, childVcs: childVcs, parentViewController: self)
        contentV.delegate = self
        return contentV
        
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //1、置UI界面
        setupUI()
        // 2、添加 titleView
        view.addSubview(pageTitleVIew)
        view.addSubview(pageContentView)
        
    }

    

}


// MARK: - 设置UI界面
extension HomeViewController {
    
    override func setupUI() {
        automaticallyAdjustsScrollViewInsets = false
        setupNavigationBar()    //设置导航栏
    }
    private func setupNavigationBar() {
        // 1.左侧
        let logoItem = UIBarButtonItem(imageName: "logo")
        // 2.右侧
        let size = CGSize(40, 40)
        let historyItem = UIBarButtonItem(imageName: "image_my_history", highlightedImage: "Image_my_history_click", size: size)
        let searchItem = UIBarButtonItem(imageName: "btn_search", highlightedImage: "btn_search_clicked", size: size)
        let scanItem = UIBarButtonItem(imageName: "Image_scan", highlightedImage: "Image_scan_click", size: size)
        
        navigationItem.leftBarButtonItem = logoItem
        navigationItem.rightBarButtonItems = [historyItem, searchItem, scanItem]
    }
}


// MARK: - 协议PageTitleViewDelegate
extension HomeViewController: PageTitleViewDelegate {
    func pageTitleView(titleVIew: PageTitleView, index: Int) {
        pageContentView.setCurrentIndex(current: index)
    }
}

// MARK: - 协议PageContetntViewDelegate
extension HomeViewController: PageContetntViewDelegate {
    func pageContetnView(contetnView: PageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        pageTitleVIew.setTitleViewProgress(progress: progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
}
